﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

namespace Delphi.Isis.Yahoo {
    public class YahooApi {
        public static List<string[]> GetCorporateAction(string yahooSymbolCode, DateTime startDate, DateTime endDate) {
            string baseUrl = "http://ichart.finance.yahoo.com/x?s=";
            
            string extUrl = string.Format("{0}&a={1}&b={2}&c={3}&d={4}&e={5}&f={6}&g=v&y=0&z=30000",
                yahooSymbolCode,
                startDate.Month - 1,
                startDate.Day,
                startDate.Year,
                endDate.Month - 1,
                endDate.Day,
                endDate.Year);
            string url = baseUrl + extUrl;

            List<string[]> corpActions = new List<string[]>();

            try {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                    // skip firstr line
                    string str = reader.ReadLine();
                    while ((str = reader.ReadLine()) != null) {
                        // split only comas when not enclosed by quotes
                        string[] result = Regex.Split(str, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
                        if (result.Length == 3) {
                            corpActions.Add(result);
                        }
                    }
                }
            } catch(Exception e) {
                return null;
            }
            return corpActions;
        }

        public static List<string[]> GetCorporateAction(string yahooSymbolCode, DateTime startDate) {
            return GetCorporateAction(yahooSymbolCode, startDate, DateTime.UtcNow);
        }


        public static List<string[]> GetQuoteHistorty(string yahooSymbolCode, DateTime startDate, DateTime endDate) {
            string baseUrl = "http://real-chart.finance.yahoo.com/table.csv?s=";
            string extUrl = string.Format("{0}&d={1}&e={2}&f={3}&g=d&a={4}&b={5}&c={6}&ignore=.csv",
                yahooSymbolCode,
                endDate.Month - 1,
                endDate.Day,
                endDate.Year,
                startDate.Month - 1,
                startDate.Day,
                startDate.Year);
            string url = baseUrl + extUrl;

       
            List<string[]> histQuotes = new List<string[]>();

            try {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                    // skip firstr line
                    string str = reader.ReadLine();
                    while ((str = reader.ReadLine()) != null) {
                        // split only comas when not enclosed by quotes
                        string[] result = Regex.Split(str, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
                        if (result.Length == 7) {
                            histQuotes.Add(result);
                        }
                    }
                }
            } catch (Exception e) {
                return null;
            }
            return histQuotes;
        }

        public static List<string[]> GetQuoteHistorty(string yahooSymbolCode, DateTime startDate) {
            return GetQuoteHistorty(yahooSymbolCode, startDate, DateTime.UtcNow);
        }
    }
}
